<template>
  <el-table :data="permissionArr" style="width: 100%; margin-bottom: 20px" row-key="id" border>
    <el-table-column label="名称" prop="name"></el-table-column>
    <el-table-column label="权限值" prop="code"></el-table-column>
    <el-table-column label="修改时间" prop="updateTime"></el-table-column>
    <el-table-column label="操作">
      <template #="{ row, $index }">
        <el-button
          type="primary"
          size="small"
          icon="Plus"
          @click="addPermission(row)"
          v-has="`btn.Permission.add`"
          :disabled="row.level == 4 ? true : false"
        >
          {{ row.level == 3 ? '添加功能' : '添加菜单' }}
        </el-button>
        <el-button
          type="primary"
          size="small"
          icon="Edit"
          @click="updatePermission(row)"
          v-has="`btn.Permission.update`"
          :disabled="row.level == 1 ? true : false"
          >编辑</el-button
        >
        <!-- <el-button type="danger" size="small" incon="Delete" :disabled="row.level == 1 ? true : false">删除</el-button> -->

        <el-popconfirm
          width="300px"
          :title="`确定要删除菜单 ${row.name} 吗？`"
          @confirm="deleteMenu(row.id)"
        >
          <template #reference>
            <el-button
              type="danger"
              size="small"
              incon="Delete"
              :disabled="row.level == 1 ? true : false"
              v-has="`btn.Permission.remove`"
              >删除</el-button
            >
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="dialogVisible" :title="menuData.id ? '更新菜单' : '添加菜单'">
    <el-form label-width="auto">
      <el-form-item label="名称">
        <el-input placeholder="请输入名称" v-model="menuData.name"></el-input>
      </el-form-item>
      <el-form-item label="权限值">
        <el-input placeholder="请输入权限值" v-model="menuData.code"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { reqAllPermission, reqAddRoUpdateMenu, reqRemoveMenu } from '@/api/acl/menu'
import type {
  PermissionResponseData,
  PermissionList,
  Permission,
  MenuParams
} from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus'

//存储菜单的数据
let permissionArr = ref<PermissionList>([])

const dialogVisible = ref<boolean>(false)

//携带的参数
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0
})

onMounted(() => {
  getHasPermission()
})

const getHasPermission = async () => {
  let result: PermissionResponseData = await reqAllPermission()
  if (result.code == 200) {
    permissionArr.value = result.data
  }
}

const addPermission = (row: Permission) => {
  //清空数据
  Object.assign(menuData, {
    id: 0,
    code: '',
    level: 0,
    name: '',
    pid: 0
  })

  dialogVisible.value = true
  //收集数据
  menuData.level = row.level + 1
  menuData.pid = row.id as number
}

const updatePermission = (row: Permission) => {
  dialogVisible.value = true
  Object.assign(menuData, row)
}

//确定按钮的回调
const save = async () => {
  let result = await reqAddRoUpdateMenu(menuData)
  if (result.code == 200) {
    dialogVisible.value = false
    //再次捞取最新的菜单数据
    getHasPermission()
    ElMessage({ type: 'success', message: menuData.id ? '更新成功' : '添加成功' })
  } else {
    ElMessage({ type: 'error', message: menuData.id ? '更新失败' : '添加失败' })
  }
}

//删除按钮的回调
const deleteMenu = async (id: number) => {
  let result = await reqRemoveMenu(id)
  if (result.code == 200) {
    ElMessage({ type: 'success', message: '删除成功' })
    //重新获取已有的菜单
    getHasPermission()
  } else {
    ElMessage({ type: 'error', message: '删除失败' })
  }
}
</script>

<style lang="scss" scoped></style>
